package com.study.forumsystemproject.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * program: forum-system-project
 * <p>
 * description: 统一返回结果类
 * <p>
 * author: xiaoxie
 * <p>
 * create: 2024-08-14 20:06
 **/
@Data
public class Result<T> {
    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private int code;
    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private String msg;
    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private T data;

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }
    public static <T> Result<T> success() {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
    }
    public static <T> Result<T> success(String msg) {
        return new Result<>(ResultCode.SUCCESS.getCode(), msg,null);
    }
    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), msg,data);
    }
    public static <T> Result<T> successJwt(T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }


    public static <T> Result<T> fail(String msg) {
        return new Result<>(ResultCode.FAILED.getCode(),msg);
    }

    public static <T> Result<T> fail(ResultCode resultCode) {
        return new Result<>(resultCode.getCode(),resultCode.getMessage());
    }
    public static <T> Result<T> fail() {
        return new Result<>(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
    }
}
